package com.ajk.server.controller.events;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.ajk.message.model.BaseModel;
import com.ajk.server.model.CreditsHistory;
import com.ajk.server.service.CreditsHistoryService;

@Component
public class CreditsListener implements ApplicationListener<CreditsEvent> {

	Logger logger = LoggerFactory.getLogger(CreditsListener.class);
	
	@Autowired
	CreditsHistoryService creditsHistoryService;
	
	@Override
	public void onApplicationEvent(CreditsEvent event) {
		try {
			CreditsHistory creditsHistory = new CreditsHistory();
			creditsHistory.setCategoryId(event.getCategory().getValue());
			creditsHistory.setInUsed(1);
			creditsHistory.setNum(event.getCategory().getNum());
			creditsHistory.setUserId(event.getUserId());
			creditsHistory.setRemark(event.getCategory().getName());
			
			if(event.getSource() instanceof Integer){
				creditsHistory.setSourceId((Integer) event.getSource());
			}
			creditsHistoryService.insertCreditsHistory(creditsHistory);
		} catch (Exception e) {
			logger.error("添加积分异常 {}",BaseModel.toJsonString(event),e);
		}
	}

}
